arrow-buffer: i256: fix ilog off-by-one when self == base - #1
Merged
Conversation
Extends the existing `test_ilog` with cases that the current tests do not cover: small results (0 and 1) for non-base-10 bases, including `self == base`. Per the std contract `n.ilog(n) == 1` (e.g. `2u32.ilog(2) == 1`), but `i256::checked_ilog` currently returns 0 for these inputs. Also cross-checks small results against `u128::ilog` as ground truth. These tests fail until the following fix is applied. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
`checked_ilog` returned the early `Some(0)` for `self == base`, but the correct result is 1 (`base^1 == base <= self`), matching the std contract `n.ilog(n) == 1`. The base-10 path was unaffected since it is handled by `checked_ilog10`, so this affected every other base (including `ilog2(2)`). Narrow the early return from `self <= base` to `self < base`; the loop already computes the correct value for `self == base`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
alamb
commented
Jun 2, 2026
| assert_eq!(i256::ZERO.checked_ilog2(), None); | ||
|
|
||
| // self == base, matches std: `n.ilog(n) == 1` | ||
| assert_eq!(i256::from(2).checked_ilog(i256::from(2)), Some(1)); |
Author
There was a problem hiding this comment.
these tests fail without the code change
theirix
approved these changes
Jun 2, 2026
theirix
marked this pull request as ready for review
June 2, 2026 18:02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
Builds on apache#9453. While reviewing that PR, I found that
i256::checked_ilog(and thereforeilog/checked_ilog2/ilog2) returns0instead of1whenself == base.What changes are included in this PR?
Are these changes tested?
Yes — see the first commit. The added assertions fail before the fix and pass after.
Are there any user-facing changes?
Fixes incorrect results from
i256::ilog/checked_ilog/ilog2/checked_ilog2whenself == base. No API changes.